/***
This do-file creates a figure comparing Current Employment Statistics (CES) data
on private sector non-farm employment with vs without seasonal adjustment. 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Seasonal Fluctuations in Employment in CES Data.yaml"

*-------------------------------------------------------------------------------
* Import CES Seasonally adjusted 
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/St Louis Fed/private_nonfarm_emp_seasonal_USPRIV.csv")
import delimited "${root}/data/dvc/St Louis Fed/private_nonfarm_emp_seasonal_USPRIV.csv", clear

gen year = real(substr(date, 1, 4))
gen month = real(substr(date, 6, 2))
rename uspriv employment_adj
assert !mi(year, month, employment_adj)

keep year month employment_adj
keep if inrange(year, 2010, 2019)

bysort year (month): gegen base = first(employment_adj)
gen employment_adj_norm = (employment_adj / base - 1) * 100
drop base

tempfile ces_adj
save `ces_adj'

*-------------------------------------------------------------------------------
* Import CES not Seasonally adjusted 
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/St Louis Fed/private_nonfarm_emp_CEU0500000001.csv")
import delimited "${root}/data/dvc/St Louis Fed/private_nonfarm_emp_CEU0500000001.csv", clear

gen year = real(substr(date, 1, 4))
gen month = real(substr(date, 6, 2))
rename ceu0500000001 employment_non_adj
assert !mi(year, month, employment_non_adj)

keep year month employment_non_adj
keep if inrange(year, 2010, 2019)

bysort year (month): gegen base = first(employment_non_adj)
gen employment_non_adj_norm = (employment_non_adj / base - 1) * 100
drop base

merge 1:1 year month using `ces_adj', assert(3) nogen

* Calculate RMSE
gen diff = employment_adj_norm - employment_non_adj_norm
gen diff2 = diff ^ 2
sum diff2 if month

local mse = `r(mean)'
local rmse: di %3.2f round(sqrt(`mse'), 0.01)

di `rmse'

gen date = mdy(month, 1, year) 
sort date

forval year = 2010/2020{
 	local thisdate = mdy(1, 1, `year')
 	local mydates `mydates' `thisdate'
}

local rmse_text_pos = mdy(6, 1, 2018)

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

tw ///
	(connected employment_non_adj_norm date, sort color(oi1) msymbol(T)) ///
	(connected employment_adj_norm date, sort color(oi2)), /// 
	xlab(`mydates', format(%tdCCYY) labsize(medsmall)) xtitle(" ") ${title_`version'} ///
	legend(order(1 "No Seasonal Adjustment"  2 "Seasonal Adjustment") size(medsmall) col(1) ring(0) pos(8) symxsize(huge)) ///
	ylabel(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	ytitle("Change in Employment (%)" "Relative to January") ///
	text(-18 `rmse_text_pos' "RMSE: `rmse' p.p." ///
	, size(medsmall) color(gs6) justification(left)) 
oi_graph_export "${root}/results/Employment/Seasonal Fluctuations in Employment in CES Data", type(${fig_type})

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

yamlout using "${root}/results/paper numbers/`category'/Seasonal Fluctuations in Employment in CES Data.yaml", ///
	key("seas_emp") ///
	comment("RMSE") ///
	value(`rmse') fmt(%9.1f)

project, creates("${root}/results/paper numbers/`category'/Seasonal Fluctuations in Employment in CES Data.yaml")
